{% extends 'basic_app/master.html' %}
{% block tag_left %}
    {% include 'oratk_app/tag_left_user.html' %}
{% endblock %}
{% block title %}
    <title>ORATK</title>
{% endblock %}
{% block userinfo %}
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
        <form name="Form" role="search" action="/oratk_app/oratk_most_box_ajax" onsubmit="return validateForm()"
              target="_blank" method="POST" id="ajax_form_sumit">
            <h3 class="page-header">查询sql执行计划</h3>
            <div class="navbar-form navbar-left">
                &nbsp 选择实例: &nbsp
                {#                <select class="form-control" name="conn_string" id="conn_string">#}
                <select class="selectpicker show-tick " style="width:450px" data-live-search="true" name="conn_string"
                        id="conn_string">

                    <option id="def">--请选择实例名--</option>
                    <optgroup label="total:{{ instanceinfo_result_total }}">
                        {% for i in instanceinfo_result %}
                            <option
                                    data-content="
                <div>{{ i.id }}.{{ i.dbname }}</div>
                <div style='padding-left: 15px;'><small>{{ i.conn_string }}</small></div>
            "
                                    value="{{ i.id }}.{{ i.dbname }}={{ i.conn_string }}">
                            </option>
                        {% endfor %}
                    </optgroup>
                </select>
                &nbsp 请输入sqlid: &nbsp
                <input type="text" style="width:220px;" name="sql_id" id="sql_id" class="form-control"
                       placeholder="sqlid">
                <button type="button" data-loading-text="Loading..." class="btn btn-default btn-primary" id="btSearch">
                    查询(本页展示）
                </button>
                <button type="submit" class="btn btn-default btn-primary" id="btSearch_new">查询(新页展示）</button>
                <div>&nbsp</div>
                <div>

                    <div class="checkbox-inline">
                        <label>
                            <input type="checkbox" name="box_val" checked
                                   readonly value="sqltext_sqlid"> sqltext_sqlid
                        </label>
                    </div>
                    <div class="checkbox-inline">
                        <label>
                            <input type="checkbox" name="box_val"
                                   value="sql_binds"> sql_binds
                        </label>
                    </div>
                    <div class="checkbox-inline">
                        <label>
                            <input type="checkbox" name="box_val"
                                   value="sql_xp_cursor"> sql_xp_cursor
                        </label>
                    </div>
                    <div class="checkbox-inline">
                        <label>
                            <input type="checkbox" name="box_val"
                                   value="sql_xp_awr"> sql_xp_awr
                        </label>
                    </div>
                    <div class="checkbox-inline">
                        <label>
                            <input type="checkbox" name="box_val"
                                   value="sqlmonitor_sqlid"> sqlmonitor_sqlid
                        </label>
                    </div>
                    {#    #执行结果#}
                    <div>&nbsp</div>
                    <div id="tab_detail"></div>
                </div>
            </div>

        </form>
    </div>
{% endblock %}

{% block script %}
    <script>
        //新页展示按钮执行前判断输入框是否有空值
        function validateForm() {
            var select_conn = $('#conn_string').find("option:checked").attr("id")
            if (select_conn == 'def') {
                alert('请选择实例')
                return false;
            }
            //判断sqlid是否为空
            var sql_id_val = $('#sql_id').val()
            if (sql_id_val == '') {
                alert('请输入 sqlid')
                return false;
            }

        }

        //当点击"查询(本页展示)时，输入框值检查及ajax异步调用"
        $('#btSearch').click(function () {
            var select_conn = $('#conn_string').find("option:checked").attr("id")
            if (select_conn == 'def') {
                alert('请选择实例')
                return;
            }

            //判断sqlid是否为空
            var sql_id_val = $('#sql_id').val()
            if (sql_id_val == '') {
                alert('请输入 sqlid')
                return;
            }
            //发送ajax请求
            let data = $('#ajax_form_sumit').serialize();
            $.ajax({
                type: "POST",
                data: data,
                url: "/oratk_app/oratk_most_box_ajax", //后台处理函数的url
                cache: false,
                dataType: "html",
                beforeSend: function () {
                    // $('#loadingModal').modal({backdrop: 'static', keyboard: false});
                    showLoading();
                    $('#btSearch').button('loading')
                },
                complete: function () {
                    //$('#loadingModal').modal('hide');
                    hideLoading();
                    $('#btSearch').button('reset')
                },
                success: function (result) {
                    $("#tab_detail").html(result);
                    //表格转文本
                    // 动态查找需要转换的表格
                    const tables = document.querySelectorAll("#tab_id_sql_xp_cursor, #tab_id_sql_xp_awr"); // 匹配多个表格
                    tables.forEach(table => {
                        const rows = table.querySelectorAll("tr");
                        let textData = "";

                        rows.forEach(row => {
                            const cells = row.querySelectorAll("td, th");
                            const rowText = Array.from(cells).map(cell => cell.innerText.trim()).join(", ");
                            if (rowText) { // 跳过空行
                                textData += rowText + "\n";
                            }
                        });
                        const textContainer = document.createElement("pre");
                        textContainer.style.width = "100%";
                        textContainer.style.overflowX = "auto";
                        textContainer.style.whiteSpace = "pre-wrap";
                        textContainer.textContent = textData.trim();
                        table.replaceWith(textContainer);
                    });

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    showconnectionAlert(XMLHttpRequest, textStatus, errorThrown)
                },
            })


        })


        function convertTableToText(win) {
            const tables = win.document.querySelectorAll("#tab_id_sql_xp_cursor, #tab_id_sql_xp_awr"); // 在新窗口查找表格
            tables.forEach(table => {
                const rows = table.querySelectorAll("tr");
                let textData = "";

                rows.forEach(row => {
                    const cells = row.querySelectorAll("td, th");
                    const rowText = Array.from(cells).map(cell => cell.innerText.trim()).join(", ");
                    if (rowText) { // 跳过空行
                        textData += rowText + "\n";
                    }
                });

                const textContainer = win.document.createElement("pre");
                textContainer.style.width = "100%";
                textContainer.style.overflowX = "auto";
                textContainer.style.whiteSpace = "pre-wrap";
                textContainer.textContent = textData.trim();
                table.replaceWith(textContainer);
            });
        }

    </script>

{% endblock %}